package typoker;

import org.directwebremoting.Browser;
import org.directwebremoting.ScriptSessions;
import org.directwebremoting.ServerContextFactory;

import typoker.logic.*;

public class GameProxy {
	private static Game game = null;
	private static Chat chat = null;

	public static synchronized void newGame() {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				if (game == null) {
					ScriptSessions.addFunctionCall("startNewGame");
				} else {
					ScriptSessions.addFunctionCall("restoreGame",
							Game.getSmallBlind(), Game.getBigBlind(), game);
					
					int[] buttons = game.getButtons();
					if (buttons != null) {
						ScriptSessions.addFunctionCall("enableButtons", buttons);
					}
				}
			}
		});
	}

	public static synchronized void startGame(String name, String image) {
		if (game == null) {
			game = new Game(name, image);
			chat = game.getChat();

			String page = ServerContextFactory.get().getContextPath()
					+ "/poker.html";
			Browser.withPage(page, new Runnable() {
				public void run() {
					ScriptSessions.addFunctionCall("restoreGame",
							Game.getSmallBlind(), Game.getBigBlind(), game);
				}
			});

			new Thread(game).start();
		}
	}

	public static synchronized void leaveGame() {
		if (game != null) {
			game.killThread();
			game = null;
			chat = null;
		}
	}

	public static synchronized void humanMove(int move) {
		if (game != null) {
			game.humanMove(move);
		}
	}
	
	public static synchronized void addMessage(String name, String msg) {
		if (chat != null) {
			chat.addMessage(name, msg);
		}
	}
	
	public static void chatMessage(final String msg) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("chatMessage", msg);
			}
		});
	}

	public static void enableButtons(final int[] buttons) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("enableButtons", buttons);
			}
		});
	}

	public static void disableButtons() {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("disableButtons");
			}
		});
	}

	public static void updatePlayer(final int id, final Player player) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("updatePlayer", id, player);
			}
		});
	}

	public static void enableActive(final int id) {
		if (game != null) {
			String page = ServerContextFactory.get().getContextPath()
					+ "/poker.html";
			Browser.withPage(page, new Runnable() {
				public void run() {
					ScriptSessions.addFunctionCall("enableActive", id);
				}
			});
		}
	}

	public static void disableActive() {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("disableActive");
			}
		});
	}

	public static void updateActive(final int id) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("updateActive", id);
			}
		});
	}

	public static void enableWinners(final int[] winners) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("enableWinners", winners);
			}
		});
	}

	public static void updateDealer(final int prevId, final int id) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("updateDealer", prevId, id);
			}
		});
	}

	public static void setRaise(final int raise) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("setRaise", raise);
			}
		});
	}

	public static void setPot(final int pot) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("setPot", pot);
			}
		});
	}

	public static void burnCard() {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("burnCard");
			}
		});
	}

	public static void dealCard(final int id, final String card, final int nr) {
		if (game != null) {
			String page = ServerContextFactory.get().getContextPath()
					+ "/poker.html";
			Browser.withPage(page, new Runnable() {
				public void run() {
					ScriptSessions.addFunctionCall("dealCard", id, card, nr);
				}
			});
		}
	}

	public static void dealCommCard(final String card, final int nr) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("dealCommCard", card, nr);
			}
		});
	}

	public static void foldCards(final int id) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("foldCards", id);
			}
		});
	}
	
	public static void showHand(final int id, final String[] hand) {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("showHand", id, hand);
			}
		});
	}

	public static void endRound() {
		String page = ServerContextFactory.get().getContextPath()
				+ "/poker.html";
		Browser.withPage(page, new Runnable() {
			public void run() {
				ScriptSessions.addFunctionCall("endRound");
			}
		});
	}
}